#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include<QTimer>
#include<QString>
#include"debug.h"
#include<QMessageBox>

MainWidget* MainWidget::instance = nullptr;

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    //初始化界面
    initUI();

    //初始化数据
    initData();

}

MainWidget::~MainWidget()
{
    delete ui;
}

MainWidget *MainWidget::getInstance()
{
    if(instance == nullptr){
        instance = new MainWidget();
    }

    return instance;
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        event->accept();
    }
    QWidget::mousePressEvent(event);
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton){
        move(event->globalPosition().toPoint() - dragPosition);
        event->accept();
    }
    QWidget::mouseMoveEvent(event);
}

MainWidget::ActiveTab MainWidget::getActiveTab()
{
    return activeTab;
}

void MainWidget::initUI()
{
    // 1. 初始化整体窗⼝布局
    initMainWindow();
    // 2. 初始化左侧
    initWindowLeft();
    // 3. 初始化中间
    initWindowMid();
    // 4. 初始化右侧
    initWindowRight();
}

void MainWidget::initMainWindow()
{
    //设置窗口宽度
    this->setMinimumSize(QSize(1020,680));
    //将窗口设置透明阴影
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    //设置图标
    this->setWindowIcon(QIcon(":/images/logo.png"));

    // 为窗口添加阴影效果
    // shadowEffect = new QGraphicsDropShadowEffect(this);
    // shadowEffect->setBlurRadius(20);        // 阴影模糊半径
    // shadowEffect->setColor(QColor(0, 0, 0, 160)); // 阴影颜色和透明度
    // shadowEffect->setOffset(0, 0);          // 阴影偏移量
    // // 应用阴影效果到窗口
    // setGraphicsEffect(shadowEffect);


    //初始化三个窗口
    mainLayout = new QHBoxLayout();
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainLayout);

    windowLeft = new QWidget();
    windowMid = new QWidget();
    windowRight = new QWidget();
    windowLeft->setObjectName("windowLeft");
    windowMid->setObjectName("windowMid");
    windowRight->setObjectName("windowRight");
    windowLeft->setFixedWidth(60);
    windowMid->setFixedWidth(210);
    // windowRight 宽度⾃适应, 不指定固定值, 只指定最⼩值
    windowRight->setMinimumWidth(700);
    mainLayout->addWidget(windowLeft);
    mainLayout->addWidget(windowMid);
    mainLayout->addWidget(windowRight);

}

void MainWidget::initWindowLeft()
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 18, 0, 0);
    layout->setSpacing(20);
    windowLeft->setLayout(layout);
    windowLeft->setStyleSheet("background-color: #EDEDED; border-top-left-radius: 5px; border-bottom-left-radius: 5px;");

    // 初始化⽤⼾头像
    userAvatar = new QPushButton();
    userAvatar->setFixedSize(45, 45);
    userAvatar->setIconSize(QSize(40, 40));
    connect(userAvatar, &QPushButton::clicked, this, [=](){
        SelfInfoWidget* selfInfoWidget = new SelfInfoWidget();
        selfInfoWidget->exec();
    });

#if TEST_UI
        userAvatar->setIcon(QIcon(":/images/default_avatar.jpg"));
#endif
        layout->addWidget(userAvatar, 1, Qt::AlignTop | Qt::AlignHCenter);

    // 初始化 tab 按钮
    sessionTabBtn = new QPushButton();
    sessionTabBtn->setFixedSize(45, 45);
    sessionTabBtn->setIcon(QIcon(":/images/session_active.png"));
    sessionTabBtn->setIconSize(QSize(30, 30));
    sessionTabBtn->setStyleSheet("QPushButton { background-color: transparent; border-radius:none;}\
                                  QPushButton:hover { background-color: #E1E1E1; }");

    layout->addWidget(sessionTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    friendTabBtn = new QPushButton();
    friendTabBtn->setFixedSize(45, 45);
    friendTabBtn->setIcon(QIcon(":/images/friend_inactive.png"));
    friendTabBtn->setIconSize(QSize(30, 30));
    friendTabBtn->setStyleSheet("QPushButton { background-color: transparent; border-radius:none;}\
                                 QPushButton:hover { background-color: #E1E1E1; }");
    layout->addWidget(friendTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    applyTabBtn = new QPushButton();
    applyTabBtn->setFixedSize(45, 45);
    applyTabBtn->setIcon(QIcon(":/images/apply_inactive.png"));
    applyTabBtn->setIconSize(QSize(30, 30));
    applyTabBtn->setStyleSheet("QPushButton { background-color: transparent; border-radius:none;}\
                                QPushButton:hover { background-color: #E1E1E1; }");
    layout->addWidget(applyTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    // 下⽅添加空隙, 把上述控件挤上去.
    layout->addStretch(20);

    // 处理 tab 按钮的点击操作
    connect(sessionTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToSession);
    connect(friendTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToFriend);
    connect(applyTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToApply);
}


void MainWidget::initWindowMid()
{
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 20, 0, 0);
    layout->setVerticalSpacing(10);
    layout->setHorizontalSpacing(0);
    windowMid->setLayout(layout);
    // 去除默认边框. 否则边框会对后续布局产⽣影响.
    windowMid->setStyleSheet("QWidget { background-color: #f7f7f7;border: none; }");

    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(30);
    searchEdit->setPlaceholderText("搜索");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; background-color: #EAEAEA; padding-left: 5px;}");

    addFriendBtn = new QPushButton();
    addFriendBtn->setFixedSize(30, 30);
    addFriendBtn->setIcon(QIcon(":/images/add.png"));
    QString style = "QPushButton { border-radius: 5px; background-color:#EAEAEA; } ";
    style += "QPushButton:hover { background-color: #E5E5E5; } ";
    addFriendBtn->setStyleSheet(style);

    connect(addFriendBtn, &QPushButton::clicked, this, [=](){
        AddFriendDialog* addFriendDialog = new AddFriendDialog();
        addFriendDialog->show();
    });

    sessionFriendArea = new SessionFriendArea();

    // 由于 GridLayout 不能够给不同的⾏设置不同的边距
    // 因此通过添加空⽩ widget 来控制搜索框和搜索按钮之间的边距
    QWidget* spacer1 = new QWidget();
    spacer1->setFixedWidth(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedWidth(10);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);
    layout->addWidget(spacer1, 0, 0);
    layout->addWidget(searchEdit, 0, 1);
    layout->addWidget(spacer2, 0, 2);
    layout->addWidget(addFriendBtn, 0, 3);
    layout->addWidget(spacer3, 0, 4);
    // 参数 1, 0, 1, 5 表⽰放在第 1 ⾏, 第 0 列, ⾏⽅向跨⼀⾏, 列⽅向跨五列.
    layout->addWidget(sessionFriendArea, 1, 0, 1, 5);

#if TEST_UI
    //测试
    for(int i = 0; i <= 50; ++i){
        sessionFriendArea->addItem(ItemType::SessionItemType, QString::number(i), QIcon(":/images/default_avatar.jpg"),"可问春风", "你好！");
    }
#endif

}

void MainWidget::initWindowRight()
{
    windowRight->setStyleSheet("background-color: #EDEDED; border-top-right-radius: 5px; border-bottom-right-radius: 5px;");

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    windowRight->setLayout(layout);

    //////////////////////////////
    ///上方标题栏
    /////////////////////////////

    QWidget* widget = new QWidget();
    widget->setObjectName("titleWidget");
    widget->setFixedHeight(65);
    widget->setStyleSheet("#titleWidget { border-bottom: 1px solid #D5D5D5; border-left: 1px solid #D5D5D5; }");
    layout->addWidget(widget, 0, Qt::AlignTop);

    QGridLayout* glayout = new QGridLayout();
    glayout->setContentsMargins(20, 0, 0, 0);
    glayout->setSpacing(0);
    widget->setLayout(glayout);

    sessionTitle = new QLabel();
    sessionTitle->setStyleSheet("QLabel { font-size: 15px; }");

#if TEST_UI
    sessionTitle->setText("这⾥是会话标题");
#endif


    //关闭按钮
    closeBtn = new QPushButton();
    closeBtn->setIcon(QIcon(":/images/close.png"));
    closeBtn->setFixedSize(40, 30);
    closeBtn->setStyleSheet("QPushButton { background-color: transparent; border: none;}\
                            QPushButton:hover { background-color: #ED4C4C; }");

    //最大化按钮
    maxBtn = new QPushButton();
    maxBtn->setIcon(QIcon(":/images/max.png"));
    maxBtn->setFixedSize(40, 30);
    maxBtn->setStyleSheet("QPushButton { background-color: transparent; border: none;}\
                            QPushButton:hover { background-color: #E1E1E1; }");

    //最小化按钮
    minBtn = new QPushButton();
    minBtn->setIcon(QIcon(":/images/min.png"));
    minBtn->setFixedSize(40, 30);
    minBtn->setStyleSheet("QPushButton { background-color: transparent; border: none;}\
                            QPushButton:hover { background-color: #E1E1E1; }");

    //详细按钮
    extraBtn = new QPushButton();
    extraBtn->setIcon(QIcon(":/images/extra.png"));
    extraBtn->setFixedSize(40, 30);
    extraBtn->setStyleSheet("QPushButton { background-color: transparent; border: none;}\
                            QPushButton:hover { background-color: #E1E1E1; }");

    connect(extraBtn, &QPushButton::clicked, this, [=]() {
        // // 测试阶段的代码
        // #if TEST_GROUP_SESSION_DETAIL
        //     GroupSessionDetailWidget* groupSessionDetailWidget = new GroupSessionDetailWidget();
        //     groupSessionDetailWidget->show();
        // #else
        //     SessionDetailWidget* sessionDetailWidget = new SessionDetailWidget(ChatSessionInfo());
        //     sessionDetailWidget->show();
        //     sessionDetailWidget->deleteLater();
        // #endif

        //获取到会话信息
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        model::ChatSessionInfo* chatSessionInfo = dataCenter->getChatSessionInfo(dataCenter->getCurrentChatSessionId());

        //如果用户id不为空表示是单聊
        if(chatSessionInfo->userId != ""){
            model::UserInfo* userInfo = dataCenter->getUserInfoByuserId(chatSessionInfo->userId);
            if(userInfo == nullptr){
                LOG() << "当前用户不存在";
                return;
            }
            //设置了关闭自动销毁，不需要手动释放内存
            SessionDetailWidget* sessionDetailWidget = new SessionDetailWidget(*chatSessionInfo);
            sessionDetailWidget->show();
        }
        else{
            GroupSessionDetailWidget* groupSessionDetailWidget = new GroupSessionDetailWidget();
            groupSessionDetailWidget->show();
        }

    });

    glayout->addWidget(sessionTitle, 0, 0, 2, 8);
    glayout->addWidget(minBtn, 0, 8, 1, 1);
    glayout->addWidget(maxBtn, 0, 9, 1, 1);
    glayout->addWidget(closeBtn, 0, 10, 1, 1);
    glayout->addWidget(extraBtn, 1, 10, 1, 1);

    connect(closeBtn, &QPushButton::clicked, this, [=](){
        close();
    });

    connect(maxBtn, &QPushButton::clicked, this, [=](){
        if(isMaximized()){
            this->showNormal();
            maxBtn->setIcon(QIcon(":/images/max.png"));
        }else{
            this->showMaximized();
            maxBtn->setIcon(QIcon(":/images/max2.png"));
        }

    });
    connect(minBtn, &QPushButton::clicked, this, [=](){
        showMinimized();
    });
    connect(extraBtn, &QPushButton::clicked, this, [=](){
        LOG() << "后面处理";
    });

    /////////////////////
    /// 初始化右侧中间, 消息显⽰区
    ////////////////////

    messageShowArea = new MessageShowArea();
    layout->addWidget(messageShowArea);
#if TEST_UI
    model::Message* message = new model::Message();
    message->sender.avatar = QIcon(":/images/default_avatar.jpg");
    message->sender.nickname = "张三";
    message->time = "2024-04-16 17:00";
    message->messageType = model::MessageType::TEXT_TYPE;
    QString text = "今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚\
                   吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃\
                   啥今晚吃啥";
                   message->content = text.toUtf8();
    messageShowArea->addMessage(false, *message);
    for (int i = 0; i < 30; i++) {
        model::Message* message = new model::Message();
        message->sender.avatar = QIcon(":/images/default_avatar.jpg");
        message->sender.nickname = "张三" + QString::number(i);
        message->time = "2024-04-16 17:00";
        message->messageType = model::MessageType::TEXT_TYPE;
        QString text = "今晚吃啥";
        message->content = text.toUtf8();
        messageShowArea->addMessage(true, *message);
    }
#endif

    /////////////////////////////////////////////////////
    /// 初始化右侧下⽅, 消息编辑区
    /////////////////////////////////////////////////////
    messageEditArea = new MessageEditArea(this);
    layout->addWidget(messageEditArea, 0, Qt::AlignBottom);
}

void MainWidget::initData()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    //初始化个人信息
    connect(dataCenter, &model::DataCenter::getMyselfInfoDone, this, [=](){
        const auto* myself = dataCenter->getMyselfInfo();
        this->userAvatar->setIcon(myself->avatar);
    });

    //在⽤⼾详情⻚修改头像时更新
    connect(dataCenter, &model::DataCenter::changeAvatarDone, this, [=]() {
        const auto* myself = dataCenter->getMyselfInfo();
        this->userAvatar->setIcon(myself->avatar);
    });

    //删除好友
    connect(dataCenter, &model::DataCenter::deleteFriendDone, this, [=] () {
        this->updateSessionList();
        this->updateFriendList();
        LOG() << "删除好友完成";
    });

    //好友申请
    connect(dataCenter, &model::DataCenter::addFriendApplyDone, this, [=]() {
        // 创建提示弹窗
        QMessageBox* msgBox = new QMessageBox(this);
        msgBox->setWindowTitle("提示");
        msgBox->setText("好友申请发送成功");
        msgBox->setIcon(QMessageBox::Information);
        msgBox->setStandardButtons(QMessageBox::NoButton); // 不显示按钮

        // 显示弹窗
        msgBox->show();

        // 设置2秒后自动关闭
        QTimer::singleShot(2000, msgBox, [msgBox]() {
            msgBox->close();
            msgBox->deleteLater(); // 释放内存
        });
    });

    //接收好友申请
    connect(dataCenter, &model::DataCenter::receiveFriendApplyDone, this, [=](){
        LOG() << "收到一个好友申请";

        updateApplyList();
    });

    //好友申请被同意
    connect(dataCenter, &model::DataCenter::receiveFriendProcessAccept, this, [=](const model::UserInfo& userInfo) {
        // 更新好友列表数据
        updateFriendList();
        // 发送全局通知
        LOG() << userInfo.nickname + " 已经同意了好友申请!";
    });

    //好友申请被拒绝
    connect(dataCenter, &model::DataCenter::receiveFriendProcessReject, this, [=](const model::UserInfo& userInfo) {
        // 发送全局通知
        LOG() << userInfo.nickname + " 已经拒绝了好友申请!";
    });

    connect(dataCenter, &model::DataCenter::clearCurrentSession, this, [=]() {
        sessionTitle->setText("");
        messageShowArea->clear();
        LOG() << "删除好友为当前会话, 清空完成";
    });

    //创建会话
    connect(dataCenter, &model::DataCenter::createChatSessionDone, this, [=]() {
        // 发送全局通知
        LOG() << "创建群聊会话请求已经发送!";
    });

    connect(dataCenter, &model::DataCenter::receiveSessionCreateDone, this, [=]() {
        // 更新会话列表数据
        this->updateSessionList();
        // 发送全局通知
        LOG() << "您被拉⼊新的群聊中!";
    });

    dataCenter->getMyselfInfoAsync();

    //初始化好友列表
    loadFriendList();

    //初始化会话列表
    loadSessionList();

    //初始化好友申请列表
    loadApplyList();
}

void MainWidget::switchTabToSession()
{
    // 1. 设置 activeTab
    activeTab = SESSION_LIST;
    // 2. 调整图标⾼亮
    sessionTabBtn->setIcon(QIcon(":/images/session_active.png"));
    friendTabBtn->setIcon(QIcon(":/images/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon(":/images/apply_inactive.png"));
    // 3. TODO 加载会话列表数据
    this->loadSessionList();
}

void MainWidget::switchTabToFriend()
{
    // 1. 设置 activeTab
    activeTab = FRIEND_LIST;
    // 2. 调整图标⾼亮
    sessionTabBtn->setIcon(QIcon(":/images/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/images/friend_active.png"));
    applyTabBtn->setIcon(QIcon(":/images/apply_inactive.png"));
    // 3. TODO 加载好友列表数据
    this->loadFriendList();
}

void MainWidget::switchTabToApply()
{
    // 1. 设置 activeTab
    activeTab = APPLY_LIST;
    // 2. 调整图标⾼亮
    sessionTabBtn->setIcon(QIcon(":/images/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/images/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon(":/images/apply_active.png"));
    // 3. TODO 加载好友列表数据
    this->loadApplyList();
}

void MainWidget::switchToSession(const QString &userId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    auto* chatSessionInfo = dataCenter->getChatSessionInfoByUserId(userId);

    if(chatSessionInfo == nullptr){
        qCritical() << TAG << "⽤⼾对应的会话不存在! userId=" << userId;
        return;
    }

    // 2. 把会话置顶
    dataCenter->topChatSessionInfo(*chatSessionInfo);

    // 3. 切换到会话标签⻚
    // 内部会根据 DataCenter 中的 chatSessionList 的顺序来排列元素.
    switchTabToSession();

    // 4. 点击⼀下这个会话
    sessionFriendArea->clickItem(0);

}

void MainWidget::loadFriendList()
{

#if LOAD_DATA_FROM_NETWORK
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(dataCenter->getFriendList() != nullptr){
        //充内存中读取数据
        updateFriendList();
    }
    else{
        //从网络获取数据
        connect(dataCenter, &model::DataCenter::getFriendListDone, this, &MainWidget::updateFriendList, Qt::UniqueConnection);
        dataCenter->getFriendListAsync();
    }
#endif
}

void MainWidget::updateFriendList()
{
    if(getActiveTab() != FRIEND_LIST){
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    const auto* friendList = dataCenter->getFriendList();

    //清空界面上的好友列表
    this->sessionFriendArea->clear();

    //将好友显示到界面上
    for(const auto& f : *friendList){
        sessionFriendArea->addItem(FriendItemType, f.userId, f.avatar, f.nickname, f.description);
    }
}

void MainWidget::loadSessionList()
{
#if LOAD_DATA_FROM_NETWORK
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(dataCenter->getSessionList() != nullptr){
        //从内存中读取数据
        updateSessionList();
    }
    else{
        //从网络获取数据
        connect(dataCenter, &model::DataCenter::getSessionListDone, this, &MainWidget::updateSessionList, Qt::UniqueConnection);
        dataCenter->getSessionListAsync();
    }
#endif
}

void MainWidget::updateSessionList()
{
    if(getActiveTab() != SESSION_LIST){
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    const auto* sessionList = dataCenter->getSessionList();

    //清空界面上的会话列表
    this->sessionFriendArea->clear();

    //将会话显示到界面上
    for (const auto& c : *sessionList) {
        if (c.prevMessage.messageType == model::TEXT_TYPE) {
            sessionFriendArea->addItem(SessionItemType, c.chatSessionId, c.avatar, c.chatSessionName, QString(c.prevMessage.content));
        } else if (c.prevMessage.messageType == model::FILE_TYPE) {
            sessionFriendArea->addItem(SessionItemType, c.chatSessionId, c.avatar, c.chatSessionName, "[⽂件]");
        } else if (c.prevMessage.messageType == model::IMAGE_TYPE) {
            sessionFriendArea->addItem(SessionItemType, c.chatSessionId, c.avatar, c.chatSessionName, "[图⽚]");
        } else if (c.prevMessage.messageType == model::SPEECH_TYPE) {
            sessionFriendArea->addItem(SessionItemType, c.chatSessionId, c.avatar, c.chatSessionName, "[语⾳]");
        } else {
            qCritical() << TAG << "messageType 错误! messageType=" << c.prevMessage.messageType;
        }
    }
}

void MainWidget::loadApplyList()
{
#if LOAD_DATA_FROM_NETWORK
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(dataCenter->getApplyList() != nullptr){
        //从内存中读取数据
        updateApplyList();
    }
    else{
        //从网络获取数据
        connect(dataCenter, &model::DataCenter::getApplyListDone, this, &MainWidget::updateApplyList, Qt::UniqueConnection);
        dataCenter->getApplyListAsync();
    }
#endif
}

void MainWidget::updateApplyList()
{
    if(getActiveTab() != APPLY_LIST){
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    const auto* applyList = dataCenter->getApplyList();

    this->sessionFriendArea->clear();

    for(const auto& a : *applyList){
        this->sessionFriendArea->addItem(ApplyItemType, a.userId, a.avatar, a.nickname, "");
    }
}

void MainWidget::loadRecentMessages(const QString& chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    if(dataCenter->getRecentMessages(chatSessionId) != nullptr){
        this->updateRencentMessages(chatSessionId);
    }
    else{
        connect(dataCenter, &model::DataCenter::getRecentMessagesDone, this, &MainWidget::updateRencentMessages);
        dataCenter->getRencentMessagesAsync(chatSessionId, true);
    }
}

void MainWidget::updateRencentMessages(const QString &chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    //获取数据
    QList<model::Message>* recentMessages = dataCenter->getRecentMessages(chatSessionId);

    this->messageShowArea->clear();

    for(int i = recentMessages->size() - 1; i >= 0; --i){
        const model::Message message = recentMessages->at(i);
        bool isLeft = message.sender.userId != dataCenter->getMyselfInfo()->userId;
        this->messageShowArea->addFrontMessage(isLeft, message);
    }

    //将滚动条滚动到末尾
    this->messageShowArea->scrollToEndLater();

    //设置会话标题
    model::ChatSessionInfo* chatSessionInfo = dataCenter->getChatSessionInfo(chatSessionId);
    if(chatSessionInfo != nullptr){
        this->sessionTitle->setText(chatSessionInfo->chatSessionName);
    }else{
        qCritical() << TAG << "chatSessionInfo 查找失败! chatSessionId=" << chatSessionId;
    }

    //保存当前被选中的会话
    dataCenter->setCurrentChatSessionId(chatSessionId);
}

MessageShowArea *MainWidget::getMessageShowArea()
{
    return messageShowArea;
}
